const ModelMusicLyric = require('../../../models/music/musicLyric')
const FileTool = require('../../../utils/filetool'),
    ComTool = require('../../../utils/commonTools');

// 删除歌词
exports.delMusicLyric = async (req, res, next) => {
    let MusicLyric = await ModelMusicLyric.findLyric(req.query);
    MusicLyric.success((value) => {
        if (value.length) {
            FileTool.removeFile(value[0].lyric_src, value[0].lyric_file_name)
            ModelMusicLyric.delLyric(req.query
                , (value) => {
                    res.render('success', {
                        data: JSON.stringify({
                            message: "删除成功",
                            status: 200,
                            affectedRows: value.affectedRows
                        })
                    })
                }, (error) => {
                    res.render('fail', {
                        data: JSON.stringify({
                            message: "删除失败",
                            status: 200,
                            error
                        })
                    })
                });

        } else {
            return false;
        }
    });
}
// 编辑歌词
exports.updateMusicLyric = async (req, res, next) => {
    let { lyric_id, lyric, lyric_file_name } = req.body;
    let MusicLyric = await ModelMusicLyric.findLyric({
        lyric_id
    });
    MusicLyric.success((value) => {
        if (value.length) {
            if (FileTool.writeLyric(value[0].lyric_src, lyric_file_name, lyric)) {
                res.render('success', {
                    data: JSON.stringify({
                        message: "更新成功",
                        status: 200,
                    })
                })
            } else {
                res.render('fail', {
                    data: JSON.stringify({
                        message: "更新失败",
                        status: 200,
                        error
                    })
                })
            }
        } else {
            return false;
        }
    });
}

/** 添加歌词 */
exports.addMusicLyric = (req, res, next) => {

    let { lyric_name, lyric } = req.body;
    let path = "./public/lyric_src/lyric_" + ComTool.getCurrentDay() + "/";
    lyric_name = lyric_name.trim() + '_' + ComTool.generate() + '.txt';

    if (!FileTool.writeLyric(path, lyric_name, lyric)) {
        res.render('fail', {
            data: JSON.stringify({
                message: "添加失败",
                status: 200,
                error: "文件保存失败"
            })
        })
    }

    let newData = {
        lyric_id: ComTool.orderCode(),
        lyric_file_name: lyric_name,
        lyric_src: path
    }

    ModelMusicLyric.addLyric(newData,
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    value: newData,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找歌词 */
exports.findMusicLyric = async (req, res, next) => {
    let MusicLyric = await ModelMusicLyric.findLyric(req.body);
    MusicLyric.success((value) => {
        if (value.length) {
            let lyrics = [];
            value.map((item, index) => {
                lyrics.push({
                    lyric_id: item.lyric_id,
                    music_name: item.music_name,
                    lyric: FileTool.readLyric(item.lyric_src, item.lyric_file_name),
                    tlyric:{
                        lyric:null
                    }
                })
            })
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到歌词",
                    value: lyrics,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到歌词",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    MusicLyric.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}

//查找所有歌词
exports.list = (req, res, next) => {
    const { Needlyric } = req.body;
    ModelMusicLyric.findAll(
        (value) => {
            let lyrics = [];
            if (Needlyric == 1) {
                value.map((item, index) => {
                    lyrics.push({
                        ...item,
                        lyric: FileTool.readLyric(item.lyric_src, item.lyric_file_name),
                        tlyric:{
                            lyric:null
                        }
                    })
                })
            }
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: lyrics.length ? lyrics : value,
                    length: value.length
                })
            })

        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}